//
//  WBHomeViewController.swift
//  WeiboSwift
//
//  Created by hfwd on 2017/5/9.
//  Copyright © 2017年 hfwd. All rights reserved.
//

import UIKit

/// 原创微博cellId
private let originalCellID = "originalCellID"

/// 原创微博cellId
private let retweetedCellID = "retweetedCellID"

class WBHomeViewController: WBBaseViewController {

    /// 微博列表视图模型
    lazy var listViewModel = WBStatusListViewModel()
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        
        setupNaviTitle()
    }
    
    /// 加载数据
    override func loadData() {
        
        print("开始加载数据")
        
        listViewModel.loadStatus(isPullup: isPullup) { (isSuccess, shouldRefresh)->() in
            
            print("结束加载数据")
            
            if self.isPullup {
                
               self.isPullup = false
        
            } else {
                
                self.refreshControl?.endRefreshing()
            }
            
            if shouldRefresh {
                
                self.tableView?.reloadData()
            }
        }
    }
    
    // MARK: - 监听点击事件
    // FIXME:未编写
    /// 显示好友
    @objc fileprivate func showFriend() {
        
        navigationController?.pushViewController(WBDemoViewController(), animated: true)
    }
}


// MARK: - UITableViewDelegate,UITableViewDataSource
extension WBHomeViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let vm = listViewModel.statusList[indexPath.row]
        
        let cellID = (vm.status.retweeted_status != nil) ? retweetedCellID : originalCellID
        //
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID, for: indexPath) as! WBStatusCell

        cell.viewModel = vm
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        let vm = listViewModel.statusList[indexPath.row]
        
        return vm.rowHeight
    }
}

// MARK: - 设置界面
extension WBHomeViewController{
    
    /// 重写父类表格
    override func setupTableView() {
        super.setupTableView()
    
        naviItem.leftBarButtonItem = UIBarButtonItem(title: "好友", fontSize: 16, target: self, action: #selector(showFriend))
        
        // 注册cell
        tableView?.register(UINib(nibName: "WBStatusNormalCell", bundle: nil), forCellReuseIdentifier: originalCellID)
        
        tableView?.register(UINib(nibName: "WBStatusRetweetedCell", bundle: nil), forCellReuseIdentifier: retweetedCellID)
        
        tableView?.rowHeight = UITableViewAutomaticDimension
        
        tableView?.estimatedRowHeight = 300
        
        tableView?.separatorStyle = .none
    }
    
    /// 设置导航栏标题
    fileprivate func setupNaviTitle() {
        
        let title = WBNetworkManager.shared.userAccount.screen_name
    
        let button = WBTitleButton(title: title)
        
        naviItem.titleView = button
        
        button.addTarget(self, action: #selector(clickTitleButton), for: .touchUpInside)
    }
    
    @objc private func clickTitleButton(btn: UIButton) {
        
        //设置选中状态
        btn.isSelected = !btn.isSelected
        
    }
}
